package com.xh.bussiness.teachbook.controller;

import com.xh.bussiness.teachbook.bean.dto.TeachingBookPlanUpOrDownDto;
import com.xh.bussiness.teachbook.bean.request.QueryTeachingBookPlanRequest;
import com.xh.bussiness.teachbook.bean.request.TeachingBookPlanDetailRequest;
import com.xh.bussiness.teachbook.bean.request.TeachingBookPlanRequest;
import com.xh.bussiness.teachbook.bean.request.TeachingBookPlanUpOrDownRequest;
import com.xh.bussiness.teachbook.model.TeachingBookPlan;
import com.xh.bussiness.teachbook.model.TeachingBookPlanDetail;
import com.xh.bussiness.teachbook.service.ITeachingBookPlanDetailService;
import com.xh.bussiness.teachbook.service.ITeachingBookPlanService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *
 * </p>
 *
 * @author Pitt zhang
 * @since 2019/9/4
 */
@Api(tags = "教案管理接口")
@RestController
@RequestMapping("/teachbook/teachingbookPlan")
public class TeachingBookPlanController extends BaseController {

    @Autowired
    private ITeachingBookPlanService teachingBookPlanService;
    @Autowired
    private ITeachingBookPlanDetailService teachingBookPlanDetailService;

    @PostMapping("/addTeachingBookPlan")
    @Transactional
    public AjaxResult addTeachingBookPlan(TeachingBookPlanRequest bean) {
        if (StringUtils.isEmpty(bean.getId())) {
            TeachingBookPlan teachingBookPlan = new TeachingBookPlan();
            BeanUtils.copyProperties(bean, teachingBookPlan);
            teachingBookPlanService.insert(teachingBookPlan);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, teachingBookPlan);
        } else {
            TeachingBookPlan teachingBookPlan = teachingBookPlanService.getById(bean.getId());
            if (teachingBookPlan == null) {
                return new AjaxResult(AjaxResultConstant.Result.ERROR, "教学计划不存在");
            }
            BeanUtils.copyProperties(bean, teachingBookPlan);
            teachingBookPlanService.update(teachingBookPlan);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, teachingBookPlan);
        }
    }

    @PostMapping("/addTeachingBookPlanDetail")
    @Transactional
    public AjaxResult addTeachingBookPlanDetail(TeachingBookPlanDetailRequest request) {
        String teachingBookPlanId = request.getTeachingBookPlanId();
        if (!StringUtils.isEmpty(teachingBookPlanId)) {
            TeachingBookPlan teachingBookPlan = teachingBookPlanService.getById(teachingBookPlanId);
            if (teachingBookPlan == null) {
                return new AjaxResult(AjaxResultConstant.Result.ERROR, "教学计划不存在");
            }
        } else {
            TeachingBookPlan teachingBookPlan = new TeachingBookPlan();
            teachingBookPlan.setRefId(request.getRefId());
            //一对一学生保存合同id
            if (request.getType().equals(1)) {
                teachingBookPlan.setType(request.getType());
                teachingBookPlan.setContractId(request.getContractId());
            }
            teachingBookPlan.setRefSubjectId(request.getRefSubjectId());
            teachingBookPlan.setRefSubjectName(request.getRefSubjectName());
            teachingBookPlanService.insert(teachingBookPlan);
            teachingBookPlanId = teachingBookPlan.getId();
        }
        if (StringUtils.isEmpty(request.getId())) {
            TeachingBookPlanDetail teachingBookPlanDetail = new TeachingBookPlanDetail();
            BeanUtils.copyProperties(request, teachingBookPlanDetail);
            teachingBookPlanDetail.setTeachingBookPlanId(teachingBookPlanId);
            teachingBookPlanDetail.setCreateTime(new Date());
            if (!StringUtils.isEmpty(request.getParentId())) {
                Integer orderNo = teachingBookPlanDetailService.queryOrderNoByParentId(request.getParentId());
                teachingBookPlanDetail.setOrderNo(orderNo);
            }
            teachingBookPlanDetailService.insert(teachingBookPlanDetail);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, teachingBookPlanDetail);
        } else {
            TeachingBookPlanDetail detail = teachingBookPlanDetailService.getById(request.getId());
            if (detail == null) {
                return new AjaxResult(AjaxResultConstant.Result.ERROR, "教学计划安排不存在");
            }
            detail.setName(request.getName());
            detail.setClasshour(request.getClasshour());
            teachingBookPlanDetailService.update(detail);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, detail);
        }
    }

    @ApiOperation(value = "计划详情上下移", notes = "计划详情上下移")
    @RequestMapping("/teachingBookPlanDetailUpOrDown")
    public AjaxResult teachingBookPlanDetailUpOrDown(@RequestBody TeachingBookPlanUpOrDownRequest request) {
        if (CollectionUtils.isEmpty(request.getDtos())) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        for (TeachingBookPlanUpOrDownDto item : request.getDtos()) {
            TeachingBookPlanDetail teachingBookPlanDetail = teachingBookPlanDetailService.getById(item.getPlanDetailId());
            if (teachingBookPlanDetail != null) {
                teachingBookPlanDetail.setOrderNo(item.getOrderNo());
                teachingBookPlanDetailService.update(teachingBookPlanDetail);
            }
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "操作成功");
    }

    @ApiOperation(value = "根据学生/小班id查询教学计划", notes = " 根据学生/小班id查询教学计划")
    @RequestMapping("/queryTeachingBookPlan")
    public AjaxResult queryTeachingBookPlan(QueryTeachingBookPlanRequest request) {
        if (request.getRefType() == null || request.getRefId() == null) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, teachingBookPlanService.queryTeachingBookPlan(request));
    }

    @ApiOperation(value = "删除学案与计划关系", notes = "删除学案与计划关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "detailPlanId", value = "计划id", required = true, dataType = "String")
    })
    @RequestMapping("/removeBindTextbookRelation")
    public AjaxResult removeBindTextbookRelation(String detailPlanId) {
        if (StringUtils.isEmpty(detailPlanId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        TeachingBookPlanDetail teachingBookPlanDetail = teachingBookPlanDetailService.getById(detailPlanId);
        if (teachingBookPlanDetail == null) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "课时安排不存在");
        }
        teachingBookPlanDetail.setTeachingBookId("");
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, teachingBookPlanDetailService.update(teachingBookPlanDetail));
    }

    @ApiOperation(value = "删除计划详情", notes = "删除学案与计划关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "detailPlanId", value = "计划id", required = true, dataType = "String")
    })
    @RequestMapping("/deleteTextbookPlanDetail")
    public AjaxResult deleteTextbookPlanDetail(String detailPlanId) {
        if (StringUtils.isEmpty(detailPlanId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        String msg = teachingBookPlanService.deleteTextbookPlanDetail(detailPlanId);
        return new AjaxResult(StringUtils.isEmpty(msg), msg);
    }

    @ApiOperation(value = "小程序查询排课计划列表", notes = "小程序查询排课计划列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lwStudentId", value = "龙文学生id", required = true, dataType = "String")
    })
    @RequestMapping("/queryTeachingBookPlanIdsByLwStudentId")
    public AjaxResult queryTeachingBookPlanIdsByLwStudentId(String lwStudentId) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, teachingBookPlanService.queryTeachingBookPlanIdsByLwStudentId(lwStudentId));
    }

    @RequestMapping("/initTeachingbookPlanOrderNo")
    public AjaxResult initTeachingbookPlanOrderNo() {
        teachingBookPlanDetailService.initOrderNo();
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

}